package com.hex.liyue.lookingdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.lookingdatemodule.bean.LookingListResultBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterLooking extends RecyclerView.Adapter<AdapterLooking.MyHolder> {
    private List<LookingListResultBean.DataBean> list;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClickListener(String appointment_id);
        void onLockGiftListener();
        void onChatListener(String fromUid,String fromAvatar,String fromName);
        void onConfimSuccess(String toUid,String appointmentId);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public AdapterLooking(List<LookingListResultBean.DataBean> list) {
        this.list = list;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_looking_list, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        final LookingListResultBean.DataBean dataBean = list.get(position);
        long invitations_time = dataBean.appointment.invitations_time * 1000;//邀约时间
        long current_time = System.currentTimeMillis();
        boolean isFinish = current_time > invitations_time;
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.appointment.launch_user.avatar)
                .error(R.drawable.icon_no_thing)
                .into(holder.iv_avater);
        holder.tv_name.setText(dataBean.appointment.launch_user.nickname);
        holder.tv_location.setText(dataBean.appointment.location);
        if (isFinish) {
            holder.tv_time.setText(DateUtils.getFormatDate(invitations_time, "yyyy年MM月dd日 EEEE HH:mm") + " 已结束");
        } else {
            holder.tv_time.setText(DateUtils.getFormatDate(invitations_time, "yyyy年MM月dd日 EEEE HH:mm") + " 开始");
        }

        holder.ll_state_1.setVisibility(View.GONE);
        holder.ll_state_2.setVisibility(View.GONE);
        holder.ll_state_3.setVisibility(View.GONE);
        holder.tv_confirm_ok.setVisibility(View.GONE);
        if (dataBean.status.equals("1")) {
            //同意
            holder.tv_state_2_info.setText("对方已接受你的应约");
            holder.ll_state_2.setVisibility(View.VISIBLE);
            holder.tv_confirm_ok.setVisibility(View.VISIBLE);
        } else if (dataBean.status.equals("2")) {
            //拒绝
            holder.ll_state_3.setVisibility(View.VISIBLE);
        } else if (dataBean.status.equals("3")) {
            //等待
            holder.ll_state_1.setVisibility(View.VISIBLE);
        } else if (dataBean.status.equals("4")) {
            //完成约会
            holder.tv_state_2_info.setText("已完成约会");
            holder.ll_state_2.setVisibility(View.VISIBLE);

        }
        holder.iv_avater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(dataBean.appointment_id);
            }
        });
        holder.tv_begin_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onChatListener(dataBean.appointment.launch_user.uid,dataBean.appointment.launch_user.avatar,dataBean.appointment.launch_user.nickname);
            }
        });
        holder.tv_confirm_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onConfimSuccess(dataBean.appointment.launch_user.uid,dataBean.appointment_id);
            }
        });
        holder.tv_look_gift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onLockGiftListener();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_avater;
        TextView tv_name, tv_time, tv_location;
        TextView tv_state_1_info, tv_state_2_info, tv_state_3_info;
        TextView tv_begin_chat, tv_confirm_ok, tv_look_gift;
        LinearLayout ll_state_1, ll_state_2, ll_state_3;

        public MyHolder(View itemView) {
            super(itemView);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_location = (TextView) itemView.findViewById(R.id.tv_location);

            tv_state_1_info = (TextView) itemView.findViewById(R.id.tv_state_1_info);
            tv_state_2_info = (TextView) itemView.findViewById(R.id.tv_state_2_info);
            tv_state_3_info = (TextView) itemView.findViewById(R.id.tv_state_3_info);

            tv_begin_chat = (TextView) itemView.findViewById(R.id.tv_begin_chat);
            tv_confirm_ok = (TextView) itemView.findViewById(R.id.tv_confirm_ok);
            tv_look_gift = (TextView) itemView.findViewById(R.id.tv_look_gift);

            ll_state_1 = (LinearLayout) itemView.findViewById(R.id.ll_state_1);
            ll_state_2 = (LinearLayout) itemView.findViewById(R.id.ll_state_2);
            ll_state_3 = (LinearLayout) itemView.findViewById(R.id.ll_state_3);
        }

    }
}
